import Vue from 'vue'
import router from '../router'
import lseapi from './api'
import lsedata from './data'
import lseloading from './loading'
let vue = new Vue()
/**
 * 检查是否上传的是图片格式文件
 * {jpg , jpeg , png , gif} 区分大小写
 */
function checkFileIsImage (file) {
  try {
    let fileName = file.name
    var suffixIndex = fileName.lastIndexOf('.')
    var src = window.URL.createObjectURL(file)
    var suffix = fileName.substring(suffixIndex + 1).toUpperCase()
    if (suffix !== 'JPG' && suffix !== 'JPEG' && suffix !== 'PNG' && suffix !== 'GIF' &&
      suffix !== 'jpg' && suffix !== 'jpeg' && suffix !== 'png' && suffix !== 'gif') {
      return false
    }
    return src
  } catch (error) {
    console.log(error)
    return false
  }
}

function checkUrlIsImg (url) {
  var suffixIndex = url.lastIndexOf('.')
  var suffix = url.substring(suffixIndex + 1).toUpperCase()
  if (suffix !== 'JPG' && suffix !== 'JPEG' && suffix !== 'PNG' && suffix !== 'GIF' &&
    suffix !== 'jpg' && suffix !== 'jpeg' && suffix !== 'png' && suffix !== 'gif') {
    return false
  }
  return true
}

function checkFileIsImageAndMp4 (file) {
  try {
    let fileName = file.name
    var suffixIndex = fileName.lastIndexOf('.')
    var src = window.URL.createObjectURL(file)
    var suffix = fileName.substring(suffixIndex + 1).toUpperCase()
    if (suffix !== 'JPG' && suffix !== 'JPEG' && suffix !== 'PNG' && suffix !== 'GIF' &&
      suffix !== 'jpg' && suffix !== 'jpeg' && suffix !== 'png' && suffix !== 'gif' && suffix !== 'mp4' && suffix !== 'MP4') {
      return false
    }
    return src
  } catch (error) {
    console.log(error)
    return false
  }
}

// z-index 需要在index.html中修改样式
function loading (text, type) {
  // let load = vue.$loading({
  //   lock: true,
  //   text: text !== null && text !== undefined ? text : ''
  //   // spinner: 'el-icon-loading'
  //   // background: 'rgba(0, 0, 0, 0.7)'
  // })
  let load = lseloading.loading({
    msg: text,
    type: type || 3
  })
  return load
}

function notice (data = {}) {
  vue.$notify({
    title: data.title !== null && data.title !== undefined ? data.title : '提示',
    dangerouslyUseHTMLString: data.isHtml !== null && data.isHtml !== undefined ? data.isHtml : false,
    type: data.type,
    duration: data.time !== null && data.time !== undefined ? data.time : 2000,
    message: data.msg
  })
}

function noticeMsg (data = {}) {
  vue.$notify({
    title: data.title !== null && data.title !== undefined ? data.title : '提示',
    type: data.type,
    showClose: false,
    duration: data.time !== null && data.time !== undefined ? data.time : 2000,
    message: data.msg
  })
}

function msg (data = {}) {
  let config = {
    content: data.msg,
    duration: data.time || 2,
    background: true
  }
  switch (data.type) {
    case 1:
      vue.$Message.success(config)
      break
    case 2:
      vue.$Message.warning(config)
      break
    case 3:
      vue.$Message.error(config)
      break
    default:
      vue.$Message.info(config)
      break
  }
}

function alert (msg, data = {}) {
  data.title = data.title || '提示'
  let modal
  switch (data.type) {
    case 'info':
      modal = vue.$Modal.info({
        title: data.title,
        content: msg,
        okText: data.okText,
        onOk: data.onOk
      })
      break
    case 'success':
      modal = vue.$Modal.success({
        title: data.title,
        content: msg,
        okText: data.okText,
        onOk: data.onOk
      })
      break
    case 'warning':
      modal = vue.$Modal.warning({
        title: data.title,
        content: msg,
        okText: data.okText,
        onOk: data.onOk
      })
      break
    case 'error':
      modal = vue.$Modal.error({
        title: data.title,
        content: msg,
        okText: data.okText,
        onOk: data.onOk
      })
      break
    default:
      modal = vue.$Modal.confirm({
        title: data.title,
        content: msg,
        okText: data.okText,
        onOk: data.onOk
      })
      break
  }
  return modal
}

function prompt (data = {}) {
  return vue.$prompt(data.msg, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: data.regex,
    inputErrorMessage: data.regexMsg
  })
}

function confirm (data = {}) {
  return vue.$confirm(data.msg, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: data.type ? data.type : 'warning'
  })
}

/**
 * 内部路由跳转
 * @param {*} path // 跳转位置（需要在router/index中配置路由）
 */
function goPath (path) {
  let token = lseapi.storageJson.get(lsedata.tokenName)
  console.log(token)
  if (token) {
    router.push({
      path: '/' + (path != null && path !== undefined ? path : '')
    })
  } else {
    msg({
      msg: '您当前未登录,无法进入LSE'
    })
  }
}
function go (path) {
  router.push({
    path: '/' + (path != null && path !== undefined ? path : '')
  })
}
function goBack () {
  router.go(-1)
}

/**
 * 时间戳转换(13为时间戳)
 * @param timestamp
 * @returns {*}
 */
function timestampToTime (timestamp) {
  // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let date = new Date(timestamp)
  let Y = date.getFullYear() + '-'
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = date.getDate() + ' '
  let h = date.getHours() + ':'
  let m = date.getMinutes() + ':'
  let s = date.getSeconds()
  return Y + M + D + h + m + s
}

export default { checkFileIsImage, loading, notice, msg, goPath, go, timestampToTime, noticeMsg, goBack, checkFileIsImageAndMp4, prompt, confirm, checkUrlIsImg, alert }
